package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.SecurityUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctInstrumentComparisonBo;
import com.rzdata.poct.system.domain.business.po.PoctInstrumentComparison;
import com.rzdata.poct.system.domain.business.po.PoctInstrumentComparisonItems;
import com.rzdata.poct.system.domain.business.vo.PoctInstrumentComparisonVo;
import com.rzdata.poct.system.mapper.PoctInstrumentComparisonMapper;
import com.rzdata.poct.system.service.IPoctInstrumentComparisonItemsService;
import com.rzdata.poct.system.service.IPoctInstrumentComparisonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 设备一致性比对Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-17
 */
@Service
public class PoctInstrumentComparisonServiceImpl extends ServicePlusImpl<PoctInstrumentComparisonMapper, PoctInstrumentComparison, PoctInstrumentComparisonVo> implements IPoctInstrumentComparisonService {

    @Autowired
    IPoctInstrumentComparisonItemsService iPoctInstrumentComparisonItemsService;

    @Override
    public PoctInstrumentComparisonVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctInstrumentComparisonVo> queryPageList(PoctInstrumentComparisonBo bo) {
        PagePlus<PoctInstrumentComparison, PoctInstrumentComparisonVo> result =
            pageVo(PageUtils.buildPagePlusNew(Long.valueOf(bo.getPageNum()), Long.valueOf(bo.getPageSize())), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public IPage<PoctInstrumentComparisonVo> queryPage(PoctInstrumentComparisonBo bo) {
        Page<PoctInstrumentComparisonVo> page = new Page<>(bo.getPageNum(), bo.getPageSize());
        return this.baseMapper.queryPage(page, bo);
    }

    @Override
    public List<PoctInstrumentComparisonVo> queryList(PoctInstrumentComparisonBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctInstrumentComparison> buildQueryWrapper(PoctInstrumentComparisonBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctInstrumentComparison> lqw = Wrappers.lambdaQuery();
        lqw.and(StringUtils.isNotBlank(bo.getBaseNo()),
             wrapper->wrapper.like(PoctInstrumentComparison::getBaseNo,bo.getBaseNo())
                              .or().like(PoctInstrumentComparison::getBaseName,bo.getBaseNo()) );
        //        lqw.eq(StringUtils.isNotBlank(bo.getBaseNo()), PoctInstrumentComparison::getBaseNo, bo.getBaseNo());
//        lqw.like(StringUtils.isNotBlank(bo.getBaseName()), PoctInstrumentComparison::getBaseName, bo.getBaseName());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseQualityNo()), PoctInstrumentComparison::getBaseQualityNo, bo.getBaseQualityNo());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseKitNo()), PoctInstrumentComparison::getBaseKitNo, bo.getBaseKitNo());
        lqw.and(StringUtils.isNotBlank(bo.getContrastNo()),
            wrapper->wrapper.like(PoctInstrumentComparison::getContrastNo,bo.getContrastNo())
                             .or().like(PoctInstrumentComparison::getContrastName,bo.getContrastNo()));
//        lqw.eq(StringUtils.isNotBlank(bo.getContrastNo()), PoctInstrumentComparison::getContrastNo, bo.getContrastNo());
//        lqw.like(StringUtils.isNotBlank(bo.getContrastName()), PoctInstrumentComparison::getContrastName, bo.getContrastName());
        lqw.eq(StringUtils.isNotBlank(bo.getContrastQualityNo()), PoctInstrumentComparison::getContrastQualityNo, bo.getContrastQualityNo());
        lqw.eq(StringUtils.isNotBlank(bo.getContrastContrastNo()), PoctInstrumentComparison::getContrastContrastNo, bo.getContrastContrastNo());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseEaValue()), PoctInstrumentComparison::getBaseEaValue, bo.getBaseEaValue());
        lqw.eq(StringUtils.isNotBlank(bo.getBaseSetValue()), PoctInstrumentComparison::getBaseSetValue, bo.getBaseSetValue());
        lqw.eq(StringUtils.isNotBlank(bo.getDifferenceValue()), PoctInstrumentComparison::getDifferenceValue, bo.getDifferenceValue());
        lqw.eq(bo.getSampleNum() != null, PoctInstrumentComparison::getSampleNum, bo.getSampleNum());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicineMinValue()), PoctInstrumentComparison::getMedicineMinValue, bo.getMedicineMinValue());
        lqw.eq(StringUtils.isNotBlank(bo.getMedicineMaxValue()), PoctInstrumentComparison::getMedicineMaxValue, bo.getMedicineMaxValue());
        lqw.eq(StringUtils.isNotBlank(bo.getRemark()), PoctInstrumentComparison::getRemark, bo.getRemark());
        lqw.apply(bo.getCreateTime()!=null,
            "date_format(create_time,'%y%m%d') = date_format({0},'%y%m%d')",
            bo.getCreateTime());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctInstrumentComparisonBo bo) {
        PoctInstrumentComparison add = BeanUtil.toBean(bo, PoctInstrumentComparison.class);
        add.setCreateBy(SecurityUtils.getUsername());
        add.setCreateTime(new Date());
        add.setDeptId(SecurityUtils.getDeptId());
        add.setStatus(1);
        validEntityBeforeSave(add);
        Boolean result = save(add);
        if (ObjectUtil.isNotEmpty(bo.getItemsList())) {
            insertItem(add.getId(), bo.getItemsList());
        }
        return result;
    }

    private void insertItem(Long id, List<PoctInstrumentComparisonItems> itemsList) {
        if (ObjectUtil.isNotEmpty(itemsList)) {
            for (PoctInstrumentComparisonItems poctInstrumentComparisonItems : itemsList) {
                poctInstrumentComparisonItems.setComparisonId(id);
                QueryWrapper<PoctInstrumentComparisonItems> queryWrapper= new QueryWrapper<>();
                queryWrapper.lambda().eq(PoctInstrumentComparisonItems:: getComparisonId, id);
                iPoctInstrumentComparisonItemsService.remove(queryWrapper);
            }
            iPoctInstrumentComparisonItemsService.saveBatch(itemsList);
        }
    }

    @Override
    public Boolean updateByBo(PoctInstrumentComparisonBo bo) {
        PoctInstrumentComparison update = BeanUtil.toBean(bo, PoctInstrumentComparison.class);
        update.setUpdateBy(SecurityUtils.getUsername());
        update.setUpdateTime(new Date());
        update.setDeptId(SecurityUtils.getDeptId());
        update.setStatus(1);
        validEntityBeforeSave(update);
        Boolean result = updateById(update);
        if (ObjectUtil.isNotEmpty(bo.getItemsList())) {
            insertItem(update.getId(), bo.getItemsList());
        }
        return result;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctInstrumentComparison entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }

    @Override
    public Boolean cancelById(Long id) {
        PoctInstrumentComparison poctInstrumentComparison = new PoctInstrumentComparison();
        poctInstrumentComparison.setStatus(0);
        poctInstrumentComparison.setUpdateTime(new Date());
        poctInstrumentComparison.setUpdateBy(SecurityUtils.getUsername());
        poctInstrumentComparison.setId(id);
        return updateById(poctInstrumentComparison);
    }
}
